#include "XMLDetector.h"
#include "convTools.h"

int XMLDetector::detectObjects(std::vector<cv::Rect> &outDetectedObjects, std::vector<double> &outConfidences, cv::Mat& inFrame)
{
	outDetectedObjects.clear();
	outConfidences.clear();
	
	//reading && applying
	if(mCurrentFrameNode)
	{
		//if(mJumpFrame == 0 || ((mJumpOffset + mCurrentFrame) % (mJumpFrame+1) == 0))
		//{
			for(TiXmlElement* lObjectElement = mCurrentFrameNode->FirstChildElement("DATObject"); lObjectElement ; lObjectElement = lObjectElement->NextSiblingElement("DATObject"))
			{
				int lID = conv2Num<int>(lObjectElement->Attribute("ID"));
				unsigned int lTmp = mMapIDCounter[lID];
				mMapIDCounter[lID]++;
				if((mJumpOffset+lTmp)%(mJumpFrame+1) != 0)
					continue;
				cv::Rect lObjectPos;
				lObjectPos.x = conv2Num<int>(lObjectElement->FirstChildElement("Position")->Attribute("x"));
				lObjectPos.y = conv2Num<int>(lObjectElement->FirstChildElement("Position")->Attribute("y"));
				lObjectPos.width = conv2Num<int>(lObjectElement->FirstChildElement("Size")->Attribute("width"));
				lObjectPos.height = conv2Num<int>(lObjectElement->FirstChildElement("Size")->Attribute("height"));

				std::string lObjectType = conv2Str(lObjectElement->FirstChildElement("LastDetection")->Attribute("object"));

				if(lObjectType == mObjectType) //Its the type of object we were looking for
					outDetectedObjects.push_back(lObjectPos);
				if(conv2Num<int>(lObjectElement->FirstChildElement("DoT")->Attribute("type"))==4)
					outConfidences.push_back(1.0);
				else
					outConfidences.push_back(conv2Num<double>(lObjectElement->FirstChildElement("LastDetection")->Attribute("confidence")));
			}
		//}
	}
	else
		return -1;
	//we take for granted that it was a new frame (the next one, specifically)
	nextFrame();
	
	return outDetectedObjects.size();	
}